package com.framework.loippi.controller.admin;

import com.framework.loippi.controller.GenericController;
import com.framework.loippi.entity.Feedback;
import com.framework.loippi.entity.User;
import com.framework.loippi.mybatis.paginator.domain.Order.Direction;
import com.framework.loippi.service.FeedbackService;
import com.framework.loippi.service.UserService;
import com.framework.loippi.support.Message;
import com.framework.loippi.support.Pageable;
import com.framework.loippi.utils.ParameterUtils;
import com.framework.loippi.utils.StringUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Controller("adminFeedbackController")
@RequestMapping("/admin/feedback")
public class FeedbackController extends GenericController {

    @Resource
    private FeedbackService feedbackService;
    @Resource
    private UserService userService;

    /**
     * 列表
     */
    @RequiresPermissions("admin:feedback:list")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public String list(HttpServletRequest request, Pageable pageable, ModelMap model) {
        Map<String, Object> paramter = ParameterUtils.getParametersMapStartingWith(request, "filter_");
        Map map = new HashMap();
        for (String key : paramter.keySet()) {
            if (!StringUtil.isEmpty(paramter.get(key).toString())) {
                map.put(key, paramter.get(key));
            }
        }
        pageable.setParameter(map);
        pageable.setOrderDirection(Direction.DESC);
        pageable.setOrderProperty("ID");
        model.addAttribute("paramter", paramter);
        model.addAttribute("page", feedbackService.findByPage(pageable));
        return "/admin/feedback/list";
    }

    /**
     * 查看
     */
    @RequestMapping(value = "/view/{id}", method = RequestMethod.GET)
    public String view(@PathVariable Long id, ModelMap model) {
        model.addAttribute("feedback", feedbackService.find(id));
        return "/admin/feedback/view";
    }

    /**
     * 删除
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public @ResponseBody
    Message delete(Long[] ids) {
        feedbackService.deleteAll(ids);
        return SUCCESS_MESSAGE;
    }

    /**
     * 标记为已处理
     */
    @RequestMapping(value = "/mark_deal", method = RequestMethod.POST)
    public @ResponseBody
    Message markDeal(Long[] ids) {
        feedbackService.markDeal(ids);
        return SUCCESS_MESSAGE;
    }

    /**
     * 标记已处理（查看列表)
     */
    @RequestMapping(value = "/reply", method = RequestMethod.POST)
    public @ResponseBody
    Message markDeal(Long id, String replyContent) {
        Feedback feedback = new Feedback();
        feedback.setId(id);
        feedback.setStatus(1);
        feedback.setReplyContent(replyContent);
        feedback.setReplyTime(new Date());
        User current = userService.getCurrent();
        feedback.setHandlerId(current.getId());
        feedback.setHandlerName(current.getNickname());
        feedback.setHandlerAccount(current.getUsername());
        feedbackService.update(feedback);
        return SUCCESS_MESSAGE;
    }

}
